Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 21, 2025

Analyzers threw ArgumentException: SyntaxTree is not part of the compilation when analyzing orchestration methods that invoke methods from referenced projects. Affects DURABLE0001-DURABLE0006, DURABLE0009-DURABLE0010.

Root cause

MethodProbeOrchestrationVisitor.FindInvokedMethods() used semanticModel.Compilation to get semantic models for invoked methods. When those methods exist in different syntax trees (e.g., referenced projects), the syntax tree isn't part of that compilation instance.

Changes

In OrchestrationAnalyzer.cs, modified FindInvokedMethods():

  • Use this.Compilation instead of semanticModel.Compilation when obtaining semantic models for callee syntax trees
  • Skip syntax trees not in the current compilation context via ContainsSyntaxTree() check
// Before
SemanticModel sm = semanticModel.SyntaxTree == calleeSyntax.SyntaxTree ?
    semanticModel : semanticModel.Compilation.GetSemanticModel(calleeSyntax.SyntaxTree);

// After
if (!this.Compilation.ContainsSyntaxTree(calleeSyntax.SyntaxTree))
{
    continue;
}

SemanticModel sm = semanticModel.SyntaxTree == calleeSyntax.SyntaxTree ?
    semanticModel : this.Compilation.GetSemanticModel(calleeSyntax.SyntaxTree);

this.Compilation is the full compilation context containing all syntax trees being analyzed, while semanticModel.Compilation may only contain a subset.

Original prompt

This section details on the original issue you should resolve

<issue_title>Analyzers throw "SyntaxTree is not part of the compilation" exceptions</issue_title>
<issue_description>I cannot get the following 6 warnings to go away when running code analysis on this project using DurableTask.

Analyzer 'Microsoft.DurableTask.Analyzers.Orchestration.DateTimeOrchestrationAnalyzer' threw an exception of type 'System.ArgumentException' with message 'SyntaxTree is not part of the compilation (Parameter 'syntaxTree')'.
Exception occurred with following context:
Compilation: <Project Name>
SyntaxTree: C:\Users\abryden\git\<project path>\TaskDispatcher.cs
SyntaxNode: [Function(nameof(OrchestrateDispatch ... [MethodDeclarationSyntax]@[2335..3145) (66,4)-(80,5)

System.ArgumentException: SyntaxTree is not part of the compilation (Parameter 'syntaxTree')
   at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetSemanticModel(SyntaxTree syntaxTree, SemanticModelOptions options)
   at Microsoft.DurableTask.Analyzers.Orchestration.MethodProbeOrchestrationVisitor.FindInvokedMethods(SemanticModel semanticModel, SyntaxNode callerSyntax, IMethodSymbol callerSymbol, String rootOrchestration, Action`1 reportDiagnostic)
   at Microsoft.DurableTask.Analyzers.Orchestration.OrchestrationAnalyzer`1.<>c__DisplayClass0_0.<Initialize>b__1(SyntaxNodeAnalysisContext ctx)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)
-----

Suppress the following diagnostics to disable this analyzer: DURABLE0001

I get one of these for each of DURABLE0001, DURABLE0002, DURABLE0003, DURABLE0004, DURABLE0005, DURABLE0006 - each the same except for the Analyzer name at the start of the message. My colleagues see the same on their own machines.

IDE: Microsoft Visual Studio Professional 2022 (64-bit) - Current - Version 17.14.21 (November 2025)

I've tried things like restarting, reinstalling the latest VS2022, removing and re-adding the nuget package Microsoft.Azure.Functions.Worker.Extensions.DurableTask, explicitly installing the Microsoft.DurableTask.Analyzers package (there's only version 0.1.0, so I can't test upgrading/downgrading this).

I've searched and found similar issues to "SyntaxTree is not part of the compilation" seemingly resolved by visual studio updates (e.g. https://developercommunity.visualstudio.com/t/Feature-Semantic-classification-is-cur/10370922), as well as previously fixed Analyzer issues (e.g. Azure/azure-functions-durable-extension#1320), so perhaps this is a new issue in a similar vein?

Thanks for your help, and sorry if this turns out not to be an issue related to this project.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix analyzers throwing syntax tree exception Fix "SyntaxTree is not part of the compilation" exception in orchestration analyzers Dec 21, 2025
Copilot AI requested a review from YunchuWang December 21, 2025 21:13
@YunchuWang YunchuWang marked this pull request as ready for review December 22, 2025 23:08
Copilot AI review requested due to automatic review settings December 22, 2025 23:08
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where Roslyn analyzers DURABLE0001-DURABLE0006, DURABLE0009-DURABLE0010 threw ArgumentException: SyntaxTree is not part of the compilation when analyzing orchestration methods that invoke methods from referenced projects.

Key Changes

  • Modified FindInvokedMethods() in MethodProbeOrchestrationVisitor to check if syntax trees are part of the compilation context before attempting to get their semantic models
  • Changed from using semanticModel.Compilation to this.Compilation to access the full compilation context containing all syntax trees being analyzed

// Since the syntax tree of the callee method might be different from the caller method, we need to get the correct semantic model,
// avoiding the exception 'Syntax node is not within syntax tree'.
// We also need to check if the syntax tree is part of the compilation to avoid 'SyntaxTree is not part of the compilation' exception.
if (!this.Compilation.ContainsSyntaxTree(calleeSyntax.SyntaxTree))
Copy link

@alainbryden alainbryden Jan 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this check only be necessary in the case that semanticModel.SyntaxTree != calleeSyntax.SyntaxTree? If that is the case, shouldn't the ternary assignment below be broken up into an if statement so that this check can be added only in the else case?

Copy link

@alainbryden alainbryden Jan 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot to check on the above comment (although some human eyes might also be good)
(edit: I guess I'm not allowed to summon the beast...)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll go a step further and suggest that since this check looks expensive, and analyzers are part of a more "performance critical" path, we should probably be sure to only perform it in the else case below where it is needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copilot is not very responsive to mentions in comment threads. you can try mention it at top level

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this contain check is doing a hashlookup of syntaxtree in current set of syntax trees -O(1) perf impact is trivial . it is used in other analyzers frequently https://github.com/dotnet/roslyn/blob/main/src/Compilers/CSharp/Portable/Compilation/CSharpCompilation.cs#L890-L894

// Since the syntax tree of the callee method might be different from the caller method, we need to get the correct semantic model,
// avoiding the exception 'Syntax node is not within syntax tree'.
// We also need to check if the syntax tree is part of the compilation to avoid 'SyntaxTree is not part of the compilation' exception.
if (!this.Compilation.ContainsSyntaxTree(calleeSyntax.SyntaxTree))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll go a step further and suggest that since this check looks expensive, and analyzers are part of a more "performance critical" path, we should probably be sure to only perform it in the else case below where it is needed.

@YunchuWang YunchuWang merged commit a6e1a8a into main Jan 4, 2026
9 checks passed
@YunchuWang YunchuWang deleted the copilot/fix-analyzer-syntax-tree-error branch January 4, 2026 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Analyzers throw "SyntaxTree is not part of the compilation" exceptions

4 participants